<?php
    /**
     * Created by PhpStorm.
     * User: dingjinlai
     * Date: 2019/4/7
     * Time: 上午10:51
     */


    class Login extends OAuth_Controller
    {
        /**
         * 登录
         * author: walker
         * Date: 2019/11/6
         * Time: 15:19
         * Note:
         */
        public function index()
        {

            if ($_POST){
                $username = htmlspecialchars(trim($this->input->post('username')));
                $password = md5(trim($this->input->post('password')));

                $this->load->library('sys_message');

                ( !$username || !$password) && $this->sys_message->warning('用户名或密码不能为空！');

                $this->load->model('admin_model');

                $user = $this->admin_model->validation_user($username, $password);


                !$user && $this->sys_message->warning('用户名或密码错误！');

                $this->load->library('session');

                $add_user = array(
                    'admin_id'   => $user['admin_id'],
                    'admin_name' => $user['admin_name'],
                    'password'   => $user['password'],
                );

                $this->session->set_userdata($add_user);

                $this->session->mark_as_temp(array('admin_id', 'admin_name', 'password'), 86400);

                $this->sys_message->success('登录成功！');

            }


            $this->load->view('login');
        }

        /**
         * 退出登录
         * author: walker
         * Date: 2019/11/6
         * Time: 15:19
         * Note:
         */
        public function logout()
        {

            $this->load->library('session');

            $this->session->unset_userdata(array(
                                               'admin_id',
                                               'admin_name',
                                               'password',
                                           ));

            redirect('/');
        }

        public function auth()
        {
            $this->load->view('admin/auth');
        }
    }
